package com.xintonghua.bussiness.ui.fragment.client;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.BackTypeBean;
import com.xintonghua.bussiness.bean.GetModeDetailsBean;
import com.xintonghua.bussiness.util.AbstractBaseAdapter;
import com.xintonghua.bussiness.util.DisplayUtils;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.ViewHolder;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liqi on 2018/3/30.
 * 获取模板详情
 */
public class GetModeDetailsActivity extends BaseActivity implements initMenu {

    @BindView(R.id.listView)
    SwipeMenuListView slistview;
    BackTypeBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_mode_details);
        ButterKnife.bind(this);
        bean = (BackTypeBean) JumpUtils.getSerializable(this);
        initUI();
    }

    AbstractBaseAdapter adapter;
    List<GetModeDetailsBean> list;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestFour:
                    MyUtils.mToast(GetModeDetailsActivity.this, "模板信息保存成功");
                    finish();
                    break;
                case MyState.requestFive:
                    list = JsonUtil.getListByJsonString(data.toString(), GetModeDetailsBean.class);
                    initSwipeMenuListView();

                    break;
                case MyState.requestSix:
                    MyUtils.mToast(GetModeDetailsActivity.this, "删除成功!");
                    getNetDataList();
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("" + bean.getName() + "模板");
        getNetDataList();
    }

    @Override
    public void getNetDataList() {
        HttpCent.getInstance(GetModeDetailsActivity.this).getRevisitNoteList(bean.getId(), GetModeDetailsActivity.this, MyState.requestFive);
    }

    int selectedPostion = 0;

    @Override
    public void initSwipeMenuListView() {
        slistview.setAdapter(new AbstractBaseAdapter<GetModeDetailsBean>(list) {
            @Override
            public View getAdapterViewAtPosition(int position, View convertView, ViewGroup parent) {
                GetModeDetailsBean bean = list.get(position);
                if (convertView == null)
                    convertView = LayoutInflater.from(GetModeDetailsActivity.this).inflate(R.layout.item_get_mode_details, null);
                TextView tv_content = ViewHolder.get(convertView, R.id.tv_content);
                tv_content.setText("" + bean.getNote());
                return convertView;
            }
        });
        slistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.putExtra("data", (Serializable) list.get(i));
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
//                // create "open" item
//                SwipeMenuItem openItem = new SwipeMenuItem(
//                        getApplicationContext());
//                // set item background
//                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
//                        0xCE)));
//                // set item width
//                openItem.setWidth(DisplayUtils.dip2px(GetModeDetailsActivity.this, 90));
//                // set item title
//                openItem.setTitle("Open");
//                // set item title fontsize
//                openItem.setTitleSize(18);
//                // set item title font color
//                openItem.setTitleColor(Color.WHITE);
//                // add to menu
//                menu.addMenuItem(openItem);
                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(DisplayUtils.dip2px(GetModeDetailsActivity.this, 90));
                // set a icon
                deleteItem.setIcon(R.drawable.ic_delete1);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        slistview.setMenuCreator(creator);
        slistview.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        HttpCent.getInstance(GetModeDetailsActivity.this).deleteModeContent(list.get(position).getId(), GetModeDetailsActivity.this, MyState.requestSix);
                        // delete
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
    }
}

interface initMenu {

    void getNetDataList();//

    void initSwipeMenuListView();
}